package org.lib.rabbitmqhello.try4;

import java.util.Date;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping(value="/try4")
@Slf4j
public class Try4Controller {

	@Autowired
	private RabbitTemplate rabbitTemplate;
	
	@GetMapping(value="/send")
	public boolean send() {
		log.info("开始send");
		IntStream.range(0, 10).forEach(i->{
			// ex4交换机=>queue4
			rabbitTemplate.convertAndSend(RabbitConfig4.EXCHANGE_4, RabbitConfig4.QUEUE_4, 
					"Try4Controller-ex4-Simple msg-" + i + ", @" + new Date());
			
			try {
				TimeUnit.SECONDS.sleep(1L);
			} catch (InterruptedException e) {
				log.info("异常：{}", e.getMessage());
			}
		});
		log.info("结束send");
		return true;
	}
	
}
